/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Leveofemployee.java
 *
 * Created on Jul 25, 2011, 11:51:03 AM
 */
package manager;

import generalCode.Connectionsql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class Leveofemployee extends javax.swing.JFrame {
    private ArrayList<LevelemployeeDTO> Lislevelemployee = new ArrayList<LevelemployeeDTO>(); 
    Vector<String> Columname;
    {
        Columname = new Vector<String>();
        Columname.add("Id");
        Columname.add("Name");
        Columname.add("Department");
        Columname.add("Leve");
        
    }
    /** Creates new form Leveofemployee */
    public Leveofemployee() throws SQLException {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(Leveofemployee.class.getName()).log(Level.SEVERE, null, ex);
        } 
        SwingUtilities.updateComponentTreeUI(this);
        initComponents();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        initData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelAssign = new javax.swing.JPanel();
        lbName = new javax.swing.JLabel();
        lbLevel = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        tfLevel = new javax.swing.JTextField();
        btOK = new javax.swing.JButton();
        btExit = new javax.swing.JButton();
        lbDepartment = new javax.swing.JLabel();
        tfDepartment = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbLevel = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelAssign.setBorder(javax.swing.BorderFactory.createTitledBorder("Assign level"));
        jPanelAssign.setLayout(null);

        lbName.setText("Name:");
        jPanelAssign.add(lbName);
        lbName.setBounds(90, 50, 31, 14);

        lbLevel.setText("Level:");
        jPanelAssign.add(lbLevel);
        lbLevel.setBounds(100, 130, 29, 14);
        jPanelAssign.add(tfName);
        tfName.setBounds(151, 46, 240, 20);
        jPanelAssign.add(tfLevel);
        tfLevel.setBounds(150, 120, 240, 20);

        btOK.setText("OK");
        jPanelAssign.add(btOK);
        btOK.setBounds(180, 160, 47, 23);

        btExit.setText("Exit");
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitActionPerformed(evt);
            }
        });
        jPanelAssign.add(btExit);
        btExit.setBounds(250, 160, 51, 23);

        lbDepartment.setText("Department:");
        jPanelAssign.add(lbDepartment);
        lbDepartment.setBounds(60, 90, 61, 14);
        jPanelAssign.add(tfDepartment);
        tfDepartment.setBounds(150, 84, 240, 20);

        tbLevel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLevel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLevelMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbLevel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelAssign, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelAssign, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbLevelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLevelMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tbLevelMouseClicked

    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btExitActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                try {
//                    new Leveofemployee().setVisible(true);
//                } catch (SQLException ex) {
//                    Logger.getLogger(Leveofemployee.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btExit;
    private javax.swing.JButton btOK;
    private javax.swing.JPanel jPanelAssign;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbDepartment;
    private javax.swing.JLabel lbLevel;
    private javax.swing.JLabel lbName;
    private javax.swing.JTable tbLevel;
    private javax.swing.JTextField tfDepartment;
    private javax.swing.JTextField tfLevel;
    private javax.swing.JTextField tfName;
    // End of variables declaration//GEN-END:variables

    private void initData() throws SQLException {
         Connectionsql db = Connectionsql.newInstance();
         String sql ="SELECT [EmployeeID],[FullName],[Deparment],[Level] FROM [Employee]";
         ResultSet rs = db.query(sql);
         while(rs.next()){
         Lislevelemployee.add(new LevelemployeeDTO(rs.getInt("employeeId"), rs.getString("fullName"), rs.getString("Deparment"), rs.getString("Level")));
         }
         db.closeConn();
         
         //step show Header on jtable
         Vector<Vector<String>>Data = new Vector<Vector<String>>();
         for(int i = 0;i<Lislevelemployee.size();i++){
         Vector<String> v = new Vector<String>();
         v.add(Lislevelemployee.get(i).getName());
         v.add(Lislevelemployee.get(i).getDepartment());
         v.add(Lislevelemployee.get(i).getLevel());
         Data.add(v);
         }
         tbLevel.setModel(new DefaultTableModel(Data,Columname));
    }
    
    //show data on table
    private void showinfor(){
    int selectedrow =-1;
    selectedrow = tbLevel.getSelectedRow();
    if(selectedrow != -1){
    tfName.setText(Lislevelemployee.get(selectedrow).getName());
    tfDepartment.setText(Lislevelemployee.get(selectedrow).getDepartment());
    tfLevel.setText(Lislevelemployee.get(selectedrow).getLevel());
    }
    
    }
    private void assignlevel() throws SQLException{
        if(validateInput()){
        Connectionsql db =Connectionsql.newInstance();
        String sql ="insert into Level("+getText(tfName)
                +","+getText(tfDepartment)
                +","+getText(tfLevel)
                +",";
        System.out.println(sql);
        if(db.update(sql)){
        JOptionPane.showMessageDialog(this,"Assign succes");
        initData();
        }
        }
        
    }

    private boolean validateInput() {
           if(tfName.getText().trim().isEmpty())
                JOptionPane.showMessageDialog(this, "input name");
       else
               if(tfDepartment.getText().trim().isEmpty())
                    JOptionPane.showMessageDialog(this, "Input Depatment");
       else
                   if(tfLevel.getText().trim().isEmpty())
                        JOptionPane.showMessageDialog(this, "Input lavel");
       return false;
    }
     private String getText(JTextField txt){
     return txt.getText().trim();
     }
}
